{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mprovenance_embeddings...\u001b[0m\n",
      "ERROR:guardrails-cli:No module named 'guardrails.hub.guardrails.provenance_embeddings'\n",
      "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mprovenance_llm...\u001b[0m\n",
      "ERROR:guardrails-cli:No module named 'guardrails.hub.guardrails.provenance_llm'\n"
     ]
    }
   ],
   "source": [
    "!guardrails hub install hub://guardrails/provenance_embeddings --quiet\n",
    "!guardrails hub install hub://guardrails/provenance_llm --quiet"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adding Provenance guardrails to the workflow\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we've seen, LLMs are great at generating structured data. Adding guardrails to that workflow gives us tighter responses and more control. While structured data is super useful as an interface between different systems, it's not always the best way to communicate with humans. For that, we need to generate unstructured text.\n",
    "\n",
    "A problem with generating unstructured text is hallucination. Hallucination occurs when an LLM generates text that is not supported by the input.\n",
    "\n",
    "Guardrails has provenance validators that detect and limit hallucinations. In this example, we show how to set them up.\n",
    "\n",
    "**Both provenance validators support validating LLM-generated text on both sentence-level and entire text level. In this demo, we'll demo how sentence-level validation works. In sentence-level validaton, each sentence in the LLM-generated text will be checked for provenance.**\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ProvenanceEmbeddings\n",
    "\n",
    "When you need to confirm that an LLM response is supported by a source text, you can use the ```ProvenanceEmbeddings``` Guardrails validator function. This function takes a list source articles, an LLM response (or prompt), and a threshold. It validates whether the response is \"close\" enough to the source articles. The threshold is a number between 0 and 1, where 1 means the response is not close to the source articles, and 0 means the response is very close to the source articles.\n",
    "\n",
    "The provenance validator function can also wrap an LLM response (similar to what we saw in the first example). It can also wrap a query function to interact with your vector database as a source. These two uses are out-of-scope for this document.\n",
    "\n",
    "For the sake of this post, let's use the following configuration for our validator:\n",
    "\n",
    "1. `sources`: List of strings, each representing a source.\n",
    "2. `embed function`: A function that takes a list of strings of length k and returns a 2D numpy array of shape (k, embedding_dim)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up our source data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sources = [\n",
    "    \"\"\"1 Move the litter box to the right location. Cats may stop using the litter box after a scary experience in the\n",
    "area, such as a loud noise or harassment by another pet. [2] They may also dislike the spot you chose after moving\n",
    "the litter box, or moving to a new home. Keep the litter box in a quiet, low-traffic spot where the cat can see\n",
    "people coming. Choose a room with at least two exits so the cat doesn't feel cornered. [3] Keep litter boxes away\n",
    "from food and water bowls. Cats do not like to combine these two areas. Signs that your cat may have had an\n",
    "unpleasant experience in the litter box including running quickly in and out of the litter box, or using an area\n",
    "near the litter box. [4] Try moving the box to a new room if you notice this. Keep at least one litter box on every\n",
    "floor of a multistory home. [5] Keep litter boxes away from food and water bowls. Cats do not like to combine these\n",
    "two areas. Signs that your cat may have had an unpleasant experience in the litter box including running quickly in\n",
    "and out of the litter box, or using an area near the litter box. [4] Try moving the box to a new room if you notice\n",
    "this. Keep at least one litter box on every floor of a multistory home. [5] 2 Play with toys near the litter box.\n",
    "Play with your cat in the same general area as the litter box. Leave toys (but not food) in the room so the cat\n",
    "spends time there and develops positive associations. [6] You can bring the cat to the litter box to investigate on\n",
    "its own, but do not drop it inside or reward it with treats for using it. These tactics can backfire by making the\n",
    "cat uncomfortable or afraid. [7] Unlike dogs, cats should choose the litter box on their own, especially if they\n",
    "used one in the past. You can bring the cat to the litter box to investigate on its own, but do not drop it inside\n",
    "or reward it with treats for using it. These tactics can backfire by making the cat uncomfortable or afraid. [7]\n",
    "Unlike dogs, cats should choose the litter box on their own, especially if they used one in the past. 3 Keep the\n",
    "litter box clean. [8] If your cat perches on the edge of the box or eliminates right next to it, the box might be\n",
    "too dirty for it. Remove clumps and top up with fresh litter at least once a day, preferably twice. Rinse the\n",
    "litter box once a week with baking soda or unscented soap. [9] If you use non-clumping litter, change the whole box\n",
    "every couple days to prevent odor build up, which can drive away the cat. [10] Do not clean the litter box with\n",
    "scented products. Do not use a disinfectant unless it is specifically made for litter boxes, as many of them\n",
    "contain chemicals toxic to cats. [11] If you use non-clumping litter, change the whole box every couple days to\n",
    "prevent odor build up, which can drive away the cat. [10] Do not clean the litter box with scented products. Do not\n",
    "use a disinfectant unless it is specifically made for litter boxes, as many of them contain chemicals toxic to\n",
    "cats. [11] 4 Switch to new litter gradually. If you bought a different kind of litter, introduce it slowly. Mix a\n",
    "little of it in with the old type, and gradually increase the proportion each time you change the litter box. [12]\n",
    "Cats usually find it easier to adjust to unscented litter with a similar texture to their old litter. [13] If the\n",
    "old type of litter is no longer available, buy two or three new types. Put them in separate litter boxes side by\n",
    "side and let the cat choose its favorite. Try adjusting the depth of the litter, especially if it has a different\n",
    "texture than the cat is used to. Many cats prefer a shallow layer of litter, less than two inches (5 cm.) deep.\n",
    "Long-haired cats often like an extra-shallow layer so they can dig to the floor of the box. [14] If the old type of\n",
    "litter is no longer available, buy two or three new types. Put them in separate litter boxes side by side and let\n",
    "the cat choose its favorite. Try adjusting the depth of the litter, especially if it has a different texture than\n",
    "the cat is used to. Many cats prefer a shallow layer of litter, less than two inches (5 cm.) deep. Long-haired cats\n",
    "often like an extra-shallow layer so they can dig to the floor of the box. [14] 5 Troubleshoot new litter boxes. If\n",
    "your cat hasn't responded well to a recent litter box replacement, try these adjustments to make it more appealing:\n",
    "[15] Some cats prefer covered boxes, and other prefer open trays. Try adding or removing the hood. Remove plastic\n",
    "liners from the litter box. These can snag a cat's claws. [16] Most cats adjust well to self-cleaning litter boxes\n",
    "but not all. There is a risk of an anxious cat being frightened by the motor, and refusing to use the box as a\n",
    "result. If in doubt it’s best to stick with a regular litter box. If the box is smaller than the old one, you\n",
    "probably need to replace it with something larger. A large box with low sides works best; some people use a plastic\n",
    "sweater box. [17] Some cats prefer covered boxes, and other prefer open trays. Try adding or removing the hood.\n",
    "Remove plastic liners from the litter box. These can snag a cat's claws. [16] Most cats adjust well to\n",
    "self-cleaning litter boxes but not all. There is a risk of an anxious cat being frightened by the motor, and\n",
    "refusing to use the box as a result. If in doubt it’s best to stick with a regular litter box. If the box is\n",
    "smaller than the old one, you probably need to replace it with something larger. A large box with low sides works\n",
    "best; some people use a plastic sweater box. [17] 6 Clean up urine and feces with an enzymatic cleaner. When the\n",
    "cat fails to use its litter box, clean the area with an enzymatic cleaner designed for cat urine (or a 10% solution\n",
    "of enzymatic washing powder in water). Rinse with cold water. This removes the smell of urine that can attract a\n",
    "cat to return to the same location. [18] For best results, lightly spray the area with rubbing alcohol after the\n",
    "water has dried. Scrub gently and let it air dry. For best results, lightly spray the area with rubbing alcohol\n",
    "after the water has dried. Scrub gently and let it air dry. 7 Make soiled areas less appealing. If your cat has\n",
    "developed a habit of going in certain locations, block its access to those areas, or find temporary ways to\n",
    "discourage it until it has relearned good habits: If the cat uses a dark hiding spot, install a bright light,\n",
    "preferably motion-activated. [19] Make rugs or other areas unpleasant to stand on with tin foil or double-sided\n",
    "tape.\"\"\",\n",
    "    \"\"\"[20] If the cat urinated on curtains, pin them up out of reach until the cat is back to using its litter.\n",
    "[21] Cover targeted furniture with plastic sheets or shower curtains. [22] Fill bathtubs and sinks with a shallow\n",
    "layer of water when not in use. [23] If the cat uses a dark hiding spot, install a bright light, preferably\n",
    "motion-activated. [19] Make rugs or other areas unpleasant to stand on with tin foil or double-sided tape. [20] If\n",
    "the cat urinated on curtains, pin them up out of reach until the cat is back to using its litter. [21] Cover\n",
    "targeted furniture with plastic sheets or shower curtains. [22] Fill bathtubs and sinks with a shallow layer of\n",
    "water when not in use. [23] 8 Place litter boxes in the problem areas. One solution is to give in to your cat's\n",
    "preferences and add new boxes in the areas it's using as a bathroom. Clearly this isn't ideal if your cat is using\n",
    "the center of the living room rug, but it's worth considering if the cat is soiling an out-of-the-way corner of\n",
    "your house. Another option is to move the cat's food bowl to this location. Most cats will not eliminate and eat in\n",
    "the same place. Another option is to move the cat's food bowl to this location. Most cats will not eliminate and\n",
    "eat in the same place. 9 Use your cat's preference to your advantage. If none of these adjustments work, you may\n",
    "have to resort to a slower transition. For example, if your cat prefers to urinate on carpet, place a piece of\n",
    "similar carpet in the litter box. If the cat is willing to use that option, add a small amount of litter on top of\n",
    "the carpet the next day. Continue adding more litter and replacing the carpet when it gets too dirty, until the cat\n",
    "is transitioned to litter completely. You may have to confine the cat to a non-carpeted area of the house for a\n",
    "while to get this option to work, or to roll up your carpets temporarily. Confinement can backfire if the cat is\n",
    "stressed or bored. Similarly, if your cat is indoor/outdoor, or if your cat used to eliminate mostly outside, add\n",
    "garden soil or sand (without any fertilizer) to the box. Again, transition slowly from sand/soil to litter by\n",
    "adding small amounts of the new substrate to the preferred substrate a little at a time. You may have to confine\n",
    "the cat to a non-carpeted area of the house for a while to get this option to work, or to roll up your carpets\n",
    "temporarily. Confinement can backfire if the cat is stressed or bored. Similarly, if your cat is indoor/outdoor, or\n",
    "if your cat used to eliminate mostly outside, add garden soil or sand (without any fertilizer) to the box. Again,\n",
    "transition slowly from sand/soil to litter by adding small amounts of the new substrate to the preferred substrate\n",
    "a little at a time. Method 2 1 Spay or neuter your cat. This is not a requirement for litter training, but it does\n",
    "make urination outside the litter box much less likely. Unneutered males are especially likely to spray urine when\n",
    "they're stressed, not getting along with another male, or demonstrating their availability to a female cat. [24]\n",
    "The sooner this happens, the more likely the behavior will stop. If it goes on too long, the habit can persist even\n",
    "after surgery. [25] The sooner this happens, the more likely the behavior will stop. If it goes on too long, the\n",
    "habit can persist even after surgery. [25] 2 Reduce stress in your cat . [26] Just like humans, cats may experience\n",
    "stress from changes in their environment or schedule. Your cat may stop using the litterbox after a person or other\n",
    "animal leaves the household, or when a new one moves in. Some cats even respond badly to redecoration. Here are a\n",
    "few ways to help: Provide private places where the cat can be on its own, including hiding spots and high perches.\n",
    "If your cat is allowed outside, let it come and go whenever it likes. [27] Let your cat initiate contact, and be\n",
    "calm and consistent in your response. Some cats are stressed because they aren't getting enough playtime, while\n",
    "others dislike being pet or picked up whenever the owner feels like it. If the cat's behavior continues, consult a\n",
    "veterinarian or animal behaviorist. Provide private places where the cat can be on its own, including hiding spots\n",
    "and high perches. If your cat is allowed outside, let it come and go whenever it likes. [27] Let your cat initiate\n",
    "contact, and be calm and consistent in your response. Some cats are stressed because they aren't getting enough\n",
    "playtime, while others dislike being pet or picked up whenever the owner feels like it. If the cat's behavior\n",
    "continues, consult a veterinarian or animal behaviorist. 3 Respond to vertical spraying. If your cat backs up\n",
    "against a vertical surface, wiggles its tail, and releases a spray of urine, your cat is spraying. If you don't see\n",
    "it in action, look for roundish areas of strong-smelling urine a little higher up than the height of your cat's\n",
    "rear end, with streak marks running down to the baseboard or floor. Any cat can perform this territorial behavior,\n",
    "but it is most common in unneutered, male cats. Here's how to respond if your cat is spraying: Spraying is often a\n",
    "response to stress or the presence of other cats. [28] Follow the advice above to address this. Spraying can be a\n",
    "response to a new neighborhood cat, especially if the spray is focused on a door, window, or air vent. Try to keep\n",
    "the cat out of your yard , or close the blinds so your cat can't see it. About 30% of cats that vets examine for\n",
    "spraying have a medical condition. [29] It's a good idea to have your cat examined, especially if you cannot find a\n",
    "solution. Spraying is often a response to stress or the presence of other cats. [28] Follow the advice above to\n",
    "address this. Spraying can be a response to a new neighborhood cat, especially if the spray is focused on a door,\n",
    "window, or air vent. Try to keep the cat out of your yard , or close the blinds so your cat can't see it. About 30%\n",
    "of cats that vets examine for spraying have a medical condition. [29] It's a good idea to have your cat examined,\n",
    "especially if you cannot find a solution. 4 Replace small boxes as a kitten grows. If you adopted your cat as a\n",
    "kitten, it may need a larger litter box once it grows up. The cat should be able to turn around comfortably, and\n",
    "still be able to find a clean spot if you miss a cleanup. [30] Cats dislike change, and may take a while to adapt\n",
    "to the new box. Follow the instructions above if problems continue. Cats dislike change, and may take a while to\n",
    "adapt to the new box. Follow the instructions above if problems continue. 5 Clip away matted fur in long-haired\n",
    "cats . Some long-haired cats dirty the fur around their rear when they eliminate. This can cause painful or\n",
    "unpleasant experiences that the cat learns to associate with the litter box. If you notice this happening,\n",
    "carefully clip away matted fur from the area. [31] 6 Minimize damage when the owner is away. Some cats react poorly\n",
    "when their owner is away. They may try to urinate somewhere with a strong scent of the owner, usually the bed.\n",
    "Instruct the pet sitter to keep the bedroom door shut, and provide extra litter boxes so the cat can always reach\n",
    "one without walking by the pet sitter. [32] If possible, hire a pet sitter the cat already knows, or at least\n",
    "introduce them before you leave. If possible, hire a pet sitter the cat already knows, or at least introduce them\n",
    "before you leave. 7 Improve behavior in multi-pet households. Urine marking is a common reaction to conflict with\n",
    "another cat or dog, which can happen even if the animals got along in the past. For best results, make sure each\n",
    "animal can access resources without approaching the other: [33] Keep one litter box for each animal, plus one\n",
    "extra. Put each one in a separate location with at least two exit routes if possible. Give each animal its own bed\n",
    "and food bowl. Keep these resources away from litter boxes and from each other. Provide plenty of perches and\n",
    "hiding spaces for each cat. Keep one litter box for each animal, plus one extra. Put each one in a separate\n",
    "location with at least two exit routes if possible. Give each animal its own bed and food bowl. Keep these\n",
    "resources away from litter boxes and from each other. Provide plenty of perches and hiding spaces for each cat. 8\n",
    "Separate animals if bad behavior continues. If your cat still won't use the litter box, or still acts aggressive to\n",
    "the other animal, try a stricter method of separation. This is often necessary when bringing a new cat into the\n",
    "household: [34] Separate the cats into rooms with a closed door in between, so they do not see each other. Gather\n",
    "each cat's facial pheromones (forehead, cheeks, chin) on a small sock or gauze pad and put each sock in the others'\n",
    "space. Then, brush each cat to collect their facial pheromones and place one cat's pheromones on the other,\n",
    "creating a group scent. Swap spaces so they can smell each other's scent in the environment. Introduce them\n",
    "carefully, providing a high place and hidey holes for escape if one is aggressive to the other. Consult a cat\n",
    "behaviorist if the cats simply can't get along. Separate the cats into rooms with a closed door in between, so they\n",
    "do not see each other. Gather each cat's facial pheromones (forehead, cheeks, chin) on a small sock or gauze pad\n",
    "and put each sock in the others' space. Then, brush each cat to collect their facial pheromones and place one cat's\n",
    "pheromones on the other, creating a group scent. Swap spaces so they can smell each other's scent in the\n",
    "environment. Introduce them carefully, providing a high place and hidey holes for escape if one is aggressive to\n",
    "the other. Consult a cat behaviorist if the cats simply can't get along. Method 3 1 Assess whether your cat is\n",
    "having a hard time urinating. If you see your cat straining while trying to urinate, or spending a long time trying\n",
    "with no result, take your cat to the vet immediately. Male cats in particular can develop urinary conditions where\n",
    "the urethra (the tube from the bladder to the penis) narrows or gets blocked. Typically small amounts of urine can\n",
    "pass until the cat is completely blocked and cannot urinate at all. This is a life threatening situation and needs\n",
    "immediate medical care. Blockages can also occur in the intestinal tract. Some cats with a urinary infection or\n",
    "blockage spend a lot of time trying to urinate, licking their genitals, or crying at their owner. [35] Some cats\n",
    "with a urinary infection or blockage spend a lot of time trying to urinate, licking their genitals, or crying at\n",
    "their owner.\"\"\",\n",
    "    \"\"\"[35] 2 Evaluate whether your cat is having fecal issues. Fecal constipation does occur in cats and can\n",
    "lead to chronic issues requiring special diets and laxatives. Diarrhea is not uncommon either, including chronic\n",
    "diarrhea related to inflammatory bowel disease. Any of these conditions are uncomfortable for the cat and may cause\n",
    "the cat to be afraid of the litter box or unable to get to the litter box in time to avoid an accident. Many cats\n",
    "with inflammatory bowel disease only have occasional symptoms for no obvious reason. Appetite changes, lethargy,\n",
    "vomiting, or increased hairball production can be signs of intestinal discomfort. Many cats with inflammatory bowel\n",
    "disease only have occasional symptoms for no obvious reason. Appetite changes, lethargy, vomiting, or increased\n",
    "hairball production can be signs of intestinal discomfort. 3 Improve physical access to the box. If your cat is\n",
    "older or has an injury, it may not be able to get into the litter box as comfortably as it once could. Does your\n",
    "cat limp, need help jumping up to a chair or bed, have episodes of shaking legs, or seem painful around its spine\n",
    "or tail? If so, take the cat to the vet immediately! You may be able to make the cat more comfortable by getting a\n",
    "litter box with lower sides, or with a \"doorway\" cut into the side. You may need to consider a larger box so that\n",
    "the cat can turn around easily once inside. An overweight cat may not fit comfortably in the box any longer. Get a\n",
    "larger box and place the cat on a diet. Consult with your veterinarian about safe weight loss strategies for your\n",
    "cat. An overweight cat may not fit comfortably in the box any longer. Get a larger box and place the cat on a diet.\n",
    "Consult with your veterinarian about safe weight loss strategies for your cat. 4 Talk to your veterinarian to rule\n",
    "out other medical problems. [36] Urinating outside of the litter box often results from a feline medical issue,\n",
    "including urinary tract infections, diabetes mellitus, chronic renal disease, hyperthyroidism, bladder inflammation\n",
    "with or without urinary crystals, and intestinal issues (IBD). When in doubt, have a veterinarian conduct a medical\n",
    "checkup. Observe your cat so you are prepared to answer your veterinarian's questions. Possible questions include:\n",
    "Does the cat urinate right by the litter box or further away? How large is the spot of urine? Does the cat ever try\n",
    "to urinate in the litter box? Does the cat vocalize when urinating? Is there an increase in water consumption? Can\n",
    "you tell if the urine is almost clear, normal color, or dark? How frequently is the cat urinating? Even if there is\n",
    "no medical cause, your vet could prescribe anti-anxiety drugs to prevent spraying. This is not a guaranteed or\n",
    "risk-free solution, so talk to your vet about the pros and cons. Observe your cat so you are prepared to answer\n",
    "your veterinarian's questions. Possible questions include: Does the cat urinate right by the litter box or further\n",
    "away? How large is the spot of urine? Does the cat ever try to urinate in the litter box? Does the cat vocalize\n",
    "when urinating? Is there an increase in water consumption? Can you tell if the urine is almost clear, normal color,\n",
    "or dark? How frequently is the cat urinating? Even if there is no medical cause, your vet could prescribe\n",
    "anti-anxiety drugs to prevent spraying. This is not a guaranteed or risk-free solution, so talk to your vet about\n",
    "the pros and cons. There are several possible reasons for this behavior. Your cat may have a medical condition that\n",
    "needs treatment from a vet. However, it's also possible that your cat is afraid of the box because something scary\n",
    "happened near it, like a loud sound or a fight with another household cat. It could also be as simple as the box is\n",
    "dirty. I would first try to figure out what's causing your cat's issue. Most cats stop using their litter box\n",
    "because they don't like the box, its location, the litter, or it's not clean enough for them. I would experiment\n",
    "until you find what works best for your cat. If your cat still isn't using the litter box, talk to your vet to rule\n",
    "out a medical issue. Cats are clean creatures that dislike soiling their living quarters. You can litter train a\n",
    "stray but it does take patience. Keep the cat in a small room with a tray in one corner, food and water in another,\n",
    "and a bed in a different corner. Given a choice between soiling their bed or food, the cat will most likely chose\n",
    "the tray.\"\"\",\n",
    "]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Create embed function**\n",
    "\n",
    "This embed funtion is a wrapper around the Cohere embed API. The function takes in a parameter called \"text\" and embeds that text. The embed function is used both for the source text as well as the output text. For the source text, the embed function is called only after Guardrails automatically chunks the text into smaller pieces. All the chunks are arranged in a list which is passed to Cohere. For the output text, the embed function is called, and then vector similarity is calculated between the source and output embeddings.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create an embedding function that uses a cohere model to embed the text\n",
    "import cohere\n",
    "import numpy as np\n",
    "from guardrails import Guard\n",
    "from guardrails.hub import ProvenanceEmbeddings\n",
    "from typing import List, Union\n",
    "import os\n",
    "\n",
    "api_key = os.environ[\"COHERE_API_KEY\"]\n",
    "\n",
    "# Create a cohere client\n",
    "cohere_client = cohere.Client(api_key=api_key)\n",
    "\n",
    "\n",
    "def embed_function(text: Union[str, List[str]]) -> np.ndarray:\n",
    "    \"\"\"Embed the text using cohere's small model.\"\"\"\n",
    "    if isinstance(text, str):  # If text is a string, wrap it in a list\n",
    "        text = [text]\n",
    "\n",
    "    response = cohere_client.embed(\n",
    "        model=\"embed-english-light-v2.0\",\n",
    "        texts=text,\n",
    "    )\n",
    "    embeddings_list = response.embeddings\n",
    "    return np.array(embeddings_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set up the rail with the ```ProvenanceEmbeddings``` validator on a single string output. The threshold set for this example is 0.3, which is more sensitive than usual. In this example, since some of the passed-in text is strongly supported by the source text, distance for those chunks is small and the result remains unchanged by Guardrails. However, some other information is not directly supporteds by the source. Guardrails took the Raw LLM output and removed the areas it deemed hallucinated.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the guard object\n",
    "guard = Guard().use(\n",
    "    ProvenanceEmbeddings(threshold=0.3, validation_method=\"sentence\", on_fail=\"fix\")\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, we show Guardrails fixes mocked out responses that hallucinated extra information that was not relevant or supported by the source text. Guardrails took the raw LLM outputs and removed the areas it deemed hallucinated.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ <span style=\"background-color: #e7dfeb\">╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">│ No messages.                                                                                            │</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ To retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits  │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ so the cat doesn’t feel cornered. Find the right litterbox for your cat, as well as the right litter.   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ Place the litterbox in a good spot, away from heavily trafficked and noisy areas. Keep the litterbox    │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ very clean, and do not punish the cat or confine her to just one room. Once training is complete, it is │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ important to clean the litter box regularly and to provide a safe and healthy environment for the cat.  │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ 'To retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ so the cat doesn’t feel cornered.\\nFind the right litterbox for your cat, as well as the right          │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ litter.\\nKeep the litterbox very clean, and do not punish the cat or confine her to just one            │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ room.\\nOnce training is complete, it is important to clean the litter box regularly and to provide a    │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ safe and healthy environment for the cat.'                                                              │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \u001b[0m\u001b[48;2;231;223;235m───────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235mNo messages.\u001b[0m\u001b[48;2;231;223;235m                                                                                           \u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╭─\u001b[0m\u001b[48;2;245;245;220m───────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m Raw LLM Output \u001b[0m\u001b[48;2;245;245;220m────────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mTo retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mso the cat doesn’t feel cornered. Find the right litterbox for your cat, as well as the right litter. \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mPlace the litterbox in a good spot, away from heavily trafficked and noisy areas. Keep the litterbox \u001b[0m\u001b[48;2;245;245;220m  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mvery clean, and do not punish the cat or confine her to just one room. Once training is complete, it is\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mimportant to clean the litter box regularly and to provide a safe and healthy environment for the cat.\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╭─\u001b[0m\u001b[48;2;240;255;240m──────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m Validated Output \u001b[0m\u001b[48;2;240;255;240m───────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m'To retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240mso the cat doesn’t feel cornered.\\nFind the right litterbox for your cat, as well as the right \u001b[0m\u001b[48;2;240;255;240m        \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240mlitter.\\nKeep the litterbox very clean, and do not punish the cat or confine her to just one \u001b[0m\u001b[48;2;240;255;240m          \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240mroom.\\nOnce training is complete, it is important to clean the litter box regularly and to provide a \u001b[0m\u001b[48;2;240;255;240m  \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240msafe and healthy environment for the cat.'\u001b[0m\u001b[48;2;240;255;240m                                                             \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Use .parse directly when we already have the LLM output\n",
    "guard.parse(\n",
    "    llm_output=\"To retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits so the cat doesn’t feel cornered. Find the right litterbox for your cat, as well as the right litter. Place the litterbox in a good spot, away from heavily trafficked and noisy areas. Keep the litterbox very clean, and do not punish the cat or confine her to just one room. Once training is complete, it is important to clean the litter box regularly and to provide a safe and healthy environment for the cat.\",\n",
    "    metadata={\"embed_function\": embed_function, \"sources\": sources},\n",
    ")\n",
    "\n",
    "# See the guard logs\n",
    "guard.history.last.tree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see here, one of the sentences was removed from the output, as it wasn't deemed relevant.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ <span style=\"background-color: #e7dfeb\">╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">│ No messages.                                                                                            │</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ Cats love lollipops and other sweet and sour foods. Cats can be retrained to use a litter box. Reward   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ them when they do with lots of candy.                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ 'Cats can be retrained to use a litter box.'                                                            │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \u001b[0m\u001b[48;2;231;223;235m───────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235mNo messages.\u001b[0m\u001b[48;2;231;223;235m                                                                                           \u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╭─\u001b[0m\u001b[48;2;245;245;220m───────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m Raw LLM Output \u001b[0m\u001b[48;2;245;245;220m────────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mCats love lollipops and other sweet and sour foods. Cats can be retrained to use a litter box. Reward \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mthem when they do with lots of candy.\u001b[0m\u001b[48;2;245;245;220m                                                                  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╭─\u001b[0m\u001b[48;2;240;255;240m──────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m Validated Output \u001b[0m\u001b[48;2;240;255;240m───────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m'Cats can be retrained to use a litter box.'\u001b[0m\u001b[48;2;240;255;240m                                                           \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# FAIL SCENARIO\n",
    "guardrail_output = guard.parse(\n",
    "    llm_output=\"Cats love lollipops and other sweet and sour foods. Cats can be retrained to use a litter box. Reward them when they do with lots of candy.\",\n",
    "    metadata={\"embed_function\": embed_function, \"sources\": sources},\n",
    ")\n",
    "\n",
    "guard.history.last.tree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarly, done here. Clearly, the other sentences were not relevant to the `sources`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ProvenanceLLM\n",
    "\n",
    "Guardrails also provides a ```ProvenanceLLM``` validator that uses evaluates provenance for LLM-generated text using an LLM. Currently, to perform this self-evaluation-based provenance check, you can pass in a name of any OpenAI ChatCompletion model like `gpt-3.5-turbo` or `gpt-4`, or pass in a callable that handles LLM calls. This callable can use any LLM, that you define. For simplicity purposes, we show here a demo of using OpenAI's `gpt-3.5-turbo` model.\n",
    "\n",
    "To use the OpenAI API, you have 3 options:\n",
    "\n",
    "- Set the OPENAI_API_KEY environment variable: os.environ[\"OPENAI_API_KEY\"] = \"[OpenAI_API_KEY]\"\n",
    "- Set the OPENAI_API_KEY using openai.api_key=\"[OpenAI_API_KEY]\"\n",
    "- Pass the api_key as a parameter to the parse function as done below, in this example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the guard object\n",
    "from guardrails.hub import ProvenanceLLM\n",
    "\n",
    "guard_1 = Guard.for_string(\n",
    "    validators=[\n",
    "        ProvenanceLLM(\n",
    "            validation_method=\"sentence\",  # can be \"sentence\" or \"full\"\n",
    "            llm_callable=\"gpt-3.5-turbo\",  # as explained above\n",
    "            top_k=3,  # number of chunks to retrieve\n",
    "            on_fail=\"fix\",\n",
    "        )\n",
    "    ],\n",
    "    description=\"testmeout\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n",
      "  warnings.warn(\n",
      "/Users/dtam/dev/guardrails/guardrails/hub/guardrails/provenance_llm/validator/main.py:227: UserWarning: The LLM returned an invalid response. Considering the sentence as unsupported...\n",
      "  warn(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ <span style=\"background-color: #e7dfeb\">╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">│ No messages.                                                                                            │</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ To retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits  │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ so the cat doesn’t feel cornered. Find the right litterbox for your cat, as well as the right litter.   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ Place the litterbox in a good spot, away from heavily trafficked and noisy areas. Keep the litterbox    │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ very clean, and do not punish the cat or confine her to just one room. Once training is complete, it is │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ important to clean the litter box regularly and to provide a safe and healthy environment for the cat.  │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ ''                                                                                                      │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \u001b[0m\u001b[48;2;231;223;235m───────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235mNo messages.\u001b[0m\u001b[48;2;231;223;235m                                                                                           \u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╭─\u001b[0m\u001b[48;2;245;245;220m───────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m Raw LLM Output \u001b[0m\u001b[48;2;245;245;220m────────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mTo retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mso the cat doesn’t feel cornered. Find the right litterbox for your cat, as well as the right litter. \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mPlace the litterbox in a good spot, away from heavily trafficked and noisy areas. Keep the litterbox \u001b[0m\u001b[48;2;245;245;220m  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mvery clean, and do not punish the cat or confine her to just one room. Once training is complete, it is\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mimportant to clean the litter box regularly and to provide a safe and healthy environment for the cat.\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╭─\u001b[0m\u001b[48;2;240;255;240m──────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m Validated Output \u001b[0m\u001b[48;2;240;255;240m───────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m''\u001b[0m\u001b[48;2;240;255;240m                                                                                                     \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Use .parse directly when we already have the LLM output\n",
    "guard_1.parse(\n",
    "    llm_output=\"To retrain a cat to use the litter box, put its litter box in a low traffic area with at least 2 exits so the cat doesn’t feel cornered. Find the right litterbox for your cat, as well as the right litter. Place the litterbox in a good spot, away from heavily trafficked and noisy areas. Keep the litterbox very clean, and do not punish the cat or confine her to just one room. Once training is complete, it is important to clean the litter box regularly and to provide a safe and healthy environment for the cat.\",\n",
    "    metadata={\"embed_function\": embed_function, \"sources\": sources},\n",
    "    api_key=os.environ[\n",
    "        \"OPENAI_API_KEY\"\n",
    "    ],  # OpenAI API key if using an OpenAI model for 'llm_callable', OR using one of the above options\n",
    ")\n",
    "\n",
    "# See the guard logs\n",
    "guard_1.history.last.tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n",
      "  warnings.warn(\n",
      "/Users/dtam/dev/guardrails/guardrails/hub/guardrails/provenance_llm/validator/main.py:227: UserWarning: The LLM returned an invalid response. Considering the sentence as unsupported...\n",
      "  warn(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ <span style=\"background-color: #e7dfeb\">╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">│ No messages.                                                                                            │</span> │\n",
       "    │ <span style=\"background-color: #e7dfeb\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ Cats love lollipops and other sweet and sour foods. Cats can be retrained to use a litter box. Reward   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">│ them when they do with lots of candy.                                                                   │</span> │\n",
       "    │ <span style=\"background-color: #f5f5dc\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">│ ''                                                                                                      │</span> │\n",
       "    │ <span style=\"background-color: #f0fff0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Logs\n",
       "└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
       "    │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \u001b[0m\u001b[48;2;231;223;235m───────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235mNo messages.\u001b[0m\u001b[48;2;231;223;235m                                                                                           \u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m │\n",
       "    │ \u001b[48;2;231;223;235m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╭─\u001b[0m\u001b[48;2;245;245;220m───────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m Raw LLM Output \u001b[0m\u001b[48;2;245;245;220m────────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mCats love lollipops and other sweet and sour foods. Cats can be retrained to use a litter box. Reward \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220mthem when they do with lots of candy.\u001b[0m\u001b[48;2;245;245;220m                                                                  \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n",
       "    │ \u001b[48;2;245;245;220m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╭─\u001b[0m\u001b[48;2;240;255;240m──────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m Validated Output \u001b[0m\u001b[48;2;240;255;240m───────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m─╮\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m''\u001b[0m\u001b[48;2;240;255;240m                                                                                                     \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n",
       "    │ \u001b[48;2;240;255;240m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n",
       "    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# FAIL SCENARIO\n",
    "guardrail_output = guard_1.parse(\n",
    "    llm_output=\"Cats love lollipops and other sweet and sour foods. Cats can be retrained to use a litter box. Reward them when they do with lots of candy.\",\n",
    "    metadata={\"embed_function\": embed_function, \"sources\": sources},\n",
    "    api_key=os.environ[\"OPENAI_API_KEY\"],\n",
    ")\n",
    "\n",
    "guard_1.history.last.tree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar outputs were observed with both validators. But, as with all LLMs, the validated outputs will change due to their stochastic nature. This demo also highlights how one can use an embedding model from Cohere along with an OpenAI LLM to get provenance.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "litellm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
